package com.opensky.qatest.ui.page;

import junit.framework.Assert;

import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.LoadableComponent;

import com.opensky.qatest.util.common.AssertReport;
import com.opensky.qatest.util.common.LogicUtils;
import com.opensky.qatest.util.selenium.WebDriverWrapper;

/*
 * OpenSky - "How it works page" model
 *
 * @author: Aliakse_Khursevich (hursevich@gmail.com)
 * @version: 1.0
 * 
 */
public class HowItWorksPage extends LoadableComponent<HowItWorksPage> implements IPage {

    // ************************ Locators ************************

    private static final String HOWITWORKS_PAGE_URL = "https://opensky.com/p/how-it-works";

    private static final String HOW_TO_1 = "//div[@id='how-to-box']//li//p[text()=\"Shopping isn't one-site fits all. Choose your own shopping team.\"]";

    private static final String HOW_TO_2 = "//div[@id='how-to-box']//li//p[text()=\"Explore and shop the unexpected and unique.\"]";

    private static final String HOW_TO_3 = "//div[@id='how-to-box']//li//p[contains(., '90-day guarantee')]";

    private static final String JOIN_BTN = "//a[@class='button join-opensky' and text()='Join OpenSky']";

    private final WebDriverWrapper driver;

    public HowItWorksPage(WebDriverWrapper driver) {
	this.driver = driver;
    }

    // ************************ Actions **************************

    @Override
    protected void load() {
	driver.get(HOWITWORKS_PAGE_URL);
    }

    public JoinPage clickJoinBtn() {

	JoinPage joinPage = new JoinPage(driver);
	
	driver.waitAndClick("JOIN_BTN", By.xpath(JOIN_BTN));

	if (joinPage.isPageLoaded()) {
	    return joinPage;
	} else {
	    return null;
	}
    }

    // ************************ Validations **************************

    @Override
    protected void isLoaded() throws Error {
	String url = driver.getCurrentUrl();
	Assert.assertTrue(AssertReport.wrongURL(HOWITWORKS_PAGE_URL, url), LogicUtils.isURLEqual(HOWITWORKS_PAGE_URL, url));
    }

    public boolean isPageLoaded() {
	return driver.waitAndVerifyURL(HOWITWORKS_PAGE_URL);
    }

    public boolean isInstructionsPresent() {
	driver.waitAndCheckElementPresent("HOW_TO_1", By.xpath(HOW_TO_1));
	driver.waitAndCheckElementPresent("HOW_TO_2", By.xpath(HOW_TO_2));
	driver.waitAndCheckElementPresent("HOW_TO_3", By.xpath(HOW_TO_3));
	return true;
    }

}
